﻿using ApiManage.Models.MongoMappings.Project;
using ApiManage.Models.RequestModels;
using ApiManage.Svc.Filter;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MongoDB.Driver;
using Rhonin.Common.Entity.Exception;
using Rhonin.Common.Entity.Page;
using Rhonin.Common.Extentions;
using System;
using System.Linq;
using System.Linq.Expressions;
using ApiManage.Applications.Mongo.Project;
using ApiManage.Service.Mongo.Project;
using ApiManage.Service.Mongo.Sys;
using Rhonin.Common.Entity.Api;

namespace ApiManage.Svc.Controllers.Project
{
    /// <inheritdoc />
    /// <summary>
    /// 项目列表
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ProjectController : BaseController
    {
        private readonly IProjectService _projectService;
        private readonly IProjectQueryService _projectQueryService;
        private readonly ISysUserQueryService _sysUserQueryService;
        /// <summary>
        /// 
        /// </summary>
        public ProjectController(IProjectService projectService,
            IProjectQueryService projectQueryService,
            ISysUserQueryService sysUserQueryService)
        {
            _projectService = projectService;
            _projectQueryService = projectQueryService;
            _sysUserQueryService = sysUserQueryService;
        }
        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [Route("GetPage"), HttpGet, CheckLogin]
        public ApiResult<PageResult<Projects>> GetPage(string name, int pageIndex, int pageSize)
        {
            var param = new QueryPage
            {
                PageIndex = pageIndex,
                PageSize = pageSize
            };
            Expression<Func<Projects, bool>> where = o => o.Status != "Delete";
            where = where.AndIf(o => o.Name.Contains(name), name.IsNotEmpty());
            var list = _projectQueryService.GetList(where);
            var user = _sysUserQueryService.GetList(o => o.Status != "Delete");
            list.ForEach(o =>
            {
                o.UserName = user.Where(m => o.User.Contains(m.Id.ToString())).Select(a => a.UserName).ToList();
            });
            var page = new PageResult<Projects>
            {
                TotalCount = list.Count,
                Items = list.OrderBy(o => o.Sort).ThenByDescending(o => o.CreateTime).Skip(param.Skip).Take(param.Limit),
                PageParameter = param
            };
            return CreateResult(page);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Save"), HttpPost, CheckLogin]
        public ApiResult Save(ProjectsExtend param)
        {
            if (string.IsNullOrEmpty(param.Name))
                throw new ApiException("名称不能为空");
            if (param.Id.IsNullOrEmpty())
            {
                _projectService.Insert(new Projects {
                    Name = param.Name,
                    Description = param.Description,
                    Url = param.Url,
                    Host = param.Host,
                    User = param.User,
                    CreateUser = UserId,
                    Status = param.Status,
                    Sort = param.Sort
                });
            }
            else
            {
                _projectService.Update(u => u.Id == new ObjectId(param.Id), Builders<Projects>.Update
                    .Set("Name", param.Name)
                    .Set("Description", param.Description)
                    .Set("Status", param.Status)
                    .Set("User", param.User)
                    .Set("Sort", param.Sort)
                    .Set("Host", param.Host)
                    .Set("Url", param.Url));
            }
            return CreateSuccess();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Remove"), HttpPost, CheckLogin]
        public ApiResult Remove(IdParam param)
        {
            try
            {
                _projectService.Delete(u => u.Id == new ObjectId(param.Id));
            }
            catch (Exception ex)
            {
                return CreateError(ex.Message);
            }
            return CreateSuccess();
        }
    }
}